package org.dfl.plugin.wizards;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.dfl.core.model.IOElement;
import org.dfl.messages.Messages;
import org.dfl.plugin.DflPlugin;
import org.dfl.plugin.editor.model.DflDiagram;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
import org.eclipse.ui.ide.IDE;
import org.jdom.Document;
import org.jdom.output.XMLOutputter;

/**
 * 1st page of a DFLNewWizard
 * @author Piotr Wlodarczyk
 *
 */
public class DflNewWizardPage extends WizardNewFileCreationPage {
	private static Logger logger = Logger.getLogger(DflNewWizardPage.class);
	
	private final IWorkbench workbench;

	/**
	 * Create a new wizard page instance.
	 * 
	 * @param workbench
	 *            the current workbench
	 * @param selection
	 *            the current object selection
	 * @see ShapesCreationWizard#init(IWorkbench, IStructuredSelection)
	 */
	DflNewWizardPage(IWorkbench workbench, IStructuredSelection selection) {
		super("shapeCreationPage1", selection); //$NON-NLS-1$
		this.workbench = workbench;
		setTitle(Messages.DflNewWizardPage_create1 + DflPlugin.DEFAULT_EXTENSION + Messages.DflNewWizardPage_file);
		setDescription(Messages.DflNewWizardPage_create1 + DflPlugin.DEFAULT_EXTENSION + Messages.DflNewWizardPage_file);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		super.createControl(parent);
		setFileName(Messages.DflNewWizardPage_sampleName+DflPlugin.DEFAULT_EXTENSION);
		setPageComplete(validatePage());
	}

	/** Return a new Diagram instance. */
	private IOElement getDefaultContent() {
		return new DflDiagram();
	}

	/**
	 * This method will be invoked, when the "Finish" button is pressed.
	 * 
	 * @see ShapesCreationWizard#performFinish()
	 */
	boolean finish() {
		// create a new file, result != null if successful
		IFile newFile = createNewFile();

		// open newly created file in the editor
		IWorkbenchPage page = workbench.getActiveWorkbenchWindow()
				.getActivePage();
		if (newFile != null && page != null) {
			try {
				IDE.openEditor(page, newFile, true);
			} catch (PartInitException e) {
				logger.error(e.getMessage(), e);
				return false;
			}
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#getInitialContents()
	 */
	protected InputStream getInitialContents() {
		ByteArrayInputStream bais = null;
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			new XMLOutputter().output(new Document(getDefaultContent().save()), baos);
			bais = new ByteArrayInputStream(baos.toByteArray());
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		return bais;
	}

	/**
	 * Return true, if the file name entered in this page is valid.
	 */
	private boolean validateFilename() {
		if (getFileName() != null
				&& getFileName().endsWith(DflPlugin.DEFAULT_EXTENSION)) {
			return true;
		}
		setErrorMessage(Messages.DflNewWizardPage_errorSufix+ DflPlugin.DEFAULT_EXTENSION);
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#validatePage()
	 */
	protected boolean validatePage() {
		return super.validatePage() && validateFilename();
	}
}